package logic;

import java.util.List;

import database.Account;
import database.DatabaseConnector;
import database.ModelClass;
import database.User;

//		This class authenticates users loging in
public class AuthenticateUser {

	private ModelClass modelclass;
	private DatabaseConnector dbConnector;
	private BitcoinExchangeRate exch;
	private List<User> results;
	private String auth;

	public AuthenticateUser(String u, String p) {

		modelclass = new ModelClass();

		dbConnector = new DatabaseConnector();
		List<User> users = modelclass.getDatabaseConnector().getUsers();
		for (User user : users) {

			if (user.getEmail().equalsIgnoreCase(u)
					&& user.getPassword().equalsIgnoreCase(p)) {

				modelclass.setCurrentUser(user);
				setModelClass(modelclass);

				for (Account account : dbConnector.getAccounts()) {
					if (account.getUserId().equals(
							modelclass.getCurrentUser().getEmail())) {
						modelclass.setCurrentAccount(account);
					}
					System.out.println(account.getBitcoinBalance());
				}

				double balancedkk = modelclass.getCurrentAccount()
						.getBitcoinBalance() * exch.btcRate() * 10;

				auth = new String("AUTH:yes:"
						+ modelclass.getCurrentUser().getName() + ":"
						+ modelclass.getCurrentAccount().getBitcoinBalance()
						+ ":" + balancedkk);
				break;

			} else {
				auth = new String("AUTH:no");

			}

		}
	}

	public String getAuth() {
		return auth;
	}

	public void setModelClass(ModelClass modelclass) {
		this.modelclass = modelclass;
	}

	public ModelClass getModelClass() {
		return modelclass;
	}
}
